package com.sjy.common.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

@Component 
public class WeChatUtils {

//    private static final String AppId = "wx53932d01d0737d62";
//
//    private static final String secret = "cb59f3d76b185ac6e9e4294bf4de5010";
    
    private static final String AppId = "wx48bc1b4b23aead4d";

    private static final String secret = "64123327cd555f3be81ed4b495b46162";
    
//    public static String access_token;
    
//    public static final String WECHAT_PUSH_URL = "http://wzscglxt.zj.chinamobile.com/wzmobile-scxtgl/operWxgh4Xgt/pushTask";
    
    private static String wechat_openid_url = "https://api.weixin.qq.com/sns/jscode2session";
    
    /**小程序获取(openid)*/
	public static String getOpenid (String code) {
		  String url = wechat_openid_url + "?" +
                "appid=" + WeChatUtils.AppId + 
                "&secret=" + WeChatUtils.secret + 
                "&js_code=" + code + 
                "&grant_type=authorization_code";
		  String restr = HttpUtil.get(url);
		  System.out.println(restr+"-=-url:"+url);
		  JSONObject json = JSONObject.parseObject(restr);	
		  return json.getString("openid");

	}
	
	 /**小程序获取(openid)*/
	public static JSONObject getOpenid (String code,String appid,String secret) {
	  String url = wechat_openid_url + "?" +
            "appid=" + appid + 
            "&secret=" + secret + 
            "&js_code=" + code + 
            "&grant_type=authorization_code";
	  String restr = HttpUtil.get(url);
	  JSONObject json = JSONObject.parseObject(restr);	
	  return json;
	} 
	
	/**小程序获取AccessToken*/
	public static String getXcxAccessToken() {
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
	            "&appid=" + AppId + 
	            "&secret=" +secret ;
		  String restr = HttpUtil.get(url);
		  JSONObject json = JSONObject.parseObject(restr);	
		  return json.getString("access_token");
	}
	
	/**公众号获取accessToken*/
	public static JSONObject getAccessToken (String code,String appid,String secret) {
	  String url = "https://api.weixin.qq.com/sns/oauth2/access_token" + "?" +
            "appid=" + appid + 
            "&secret=" + secret + 
            "&code=" + code + 
            "&grant_type=authorization_code";
	  String restr = HttpUtil.get(url);
	  System.out.println(url+"=======restr:"+restr);
	  JSONObject json = JSONObject.parseObject(restr);	
	  return json;
	} 
	
	/**公众号获取accessToken*/
	public static String getAccessToken () {
	  String url = "https://api.weixin.qq.com/cgi-bin/token" + "?" +
            "appid=" + AppId + 
            "&secret=" + secret + 
            "&grant_type=client_credential";
	  String restr = HttpUtil.get(url);
	  JSONObject json = JSONObject.parseObject(restr);	
	  return json.getString("access_token");
	} 
	/**公众号获取用户信息*/
	public static JSONObject getUserInfo (String openid,String accessToken) {
	  String url = "https://api.weixin.qq.com/sns/userinfo" + 
            "?access_token=" + accessToken + 
            "&openid=" + openid + 
            "&lang=zh_CN";
	  String restr = HttpUtil.get(url);
	  System.out.println(url+"=======restr:"+restr);
	  JSONObject json = JSONObject.parseObject(restr);	
	  return json;
	} 
	
	
	/**公众号根据code获取用户信息*/
	public static JSONObject getUserInfoByCode (String code) {
		//获取accessToken&&获取openId
		JSONObject json = getAccessToken(code, AppId, secret);
		String accessToken = json.getString("access_token");
		String openid =  json.getString("openid");
		return getUserInfo(openid, accessToken);
	} 
	
	/**小程序根据code获取openId*/
	public static JSONObject getXcxOpenIdByCode (String code) {
		//获取accessToken&&获取openId
		JSONObject json = getOpenid(code, AppId, secret);
		
		String accessToken = getXcxAccessToken();
		String openid =  json.getString("openid");
		return getUserInfo(openid, accessToken);
	} 
	
	
	public static HttpResponse getwxacode(Long placeId,String accessToken,String path,String env_version,Integer width,boolean auto_color,Object line_color,boolean is_hyaline) {
		String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("path", path);
		paramMap.put("access_token", accessToken);
		//String restr = HttpUtil.createPost(url);
		//https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=
		HttpResponse response = HttpUtil.createPost("https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken)
				.header("Content-Type", "image/jpeg").charset("utf-8").body("{\"scene\":\""+placeId+"\",\"path\":\""+path+"\"}")
				.execute();
		return response;
	}
	public static HttpResponse getwxacode2(Long placeId,String accessToken,String path) {
		String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;
//		Map<String,Object> paramMap = new HashMap<String,Object>();
//		paramMap.put("path", path);
//		paramMap.put("access_token", accessToken);
		//String restr = HttpUtil.createPost(url);
		//https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=
		HttpResponse response = HttpUtil.createPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken)
				.header("Content-Type", "image/jpeg").charset("utf-8").body("{\"scene\":\""+placeId+"\",\"path\":\""+path+"\"}")
				.execute();
		return response;
	}
}
